//
//  TY_DataCommand.m
//  TY-C
//
//  Created by xyyl on 2024/5/17.
//

#import "TY_DataCommand.h"

@implementation TY_DataCommand

+ (NSArray <People *> *)checkAllData {
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    NSPersistentContainer *container = delegate.persistentContainer;
    
    // ++++++++ 查询数据 ++++++++
        NSFetchRequest *request = [[NSFetchRequest alloc] init];
        //设置要查询的实体：
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"People" inManagedObjectContext:container.viewContext];
        request.entity = entity;
        NSError *error = nil;
        NSArray *objs = [container.viewContext executeFetchRequest:request error:&error];
        if (error) {
            [NSException raise:@"查询错误" format:@"%@", [error localizedDescription]];
        }
        //遍历数据：
        for (People *obj in objs) {
            NSLog(@"%@",obj.cflx);

        }
//    [container.viewContext deleteObject:obj];

//        [container.viewContext save:nil];//保存

    
    return objs;

    
}

+ (void)delDataWithCreateID:(NSString *)createId {
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    NSPersistentContainer *container = delegate.persistentContainer;
    
    //建立请求，连接实体
    NSFetchRequest *request = [[NSFetchRequest alloc] init] ;
    NSEntityDescription *person = [NSEntityDescription entityForName:@"People" inManagedObjectContext:container.viewContext];
    request.entity = person;
    
    //设置条件过滤（搜索name属性中包含”lifengfeng“的那条记录，注意等号必须加，可以有空格，也可以是==）
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"creatTime==%@", createId];
    request.predicate = predicate;
    
    //遍历所有实体，将每个实体的信息存放在数组中
    NSArray *arr = [container.viewContext executeFetchRequest:request error:nil];
    
    //删除并保存
    if(arr.count)
    {
        for (People *p in arr)
        {
            [container.viewContext deleteObject:p];
            [self tips:@"删除成功"];

        }
        //保存
        [container.viewContext save:nil];
    }
}

+ (void)updateDataWithChufangModel:(ChufangModel *)chufangModel {
    //建立请求，连接实体
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSPersistentContainer *container = delegate.persistentContainer;
    
    NSFetchRequest *request = [[NSFetchRequest alloc] init] ;
    NSEntityDescription *person = [NSEntityDescription entityForName:@"People" inManagedObjectContext:container.viewContext];
    request.entity = person;

    
    //设置条件过滤（搜索所有name属性不为“lifengfeng”的数据）
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"creatTime==%@", chufangModel.creatTime];
    request.predicate = predicate;
    //遍历所有实体，将每个实体的信息存放在数组中
    NSArray *arr = [container.viewContext executeFetchRequest:request error:nil];
    //更改并保存
    if(arr.count) {
        for (People *mm in arr) {
             mm.cfmc = chufangModel.cfmc;
             mm.cflx = chufangModel.cflx;
             mm.pl = chufangModel.pl;
             mm.mk = chufangModel.mk;
             mm.zlsj = chufangModel.zlsj;
             mm.cxsj = chufangModel.cxsj;
             mm.jgsj = chufangModel.jgsj;
             mm.sssj = chufangModel.sssj;
             mm.xjsj = chufangModel.xjsj;
             mm.ycsj = chufangModel.ycsj;
             mm.isfscs = chufangModel.isfscs;
             mm.isbj = chufangModel.isbj;
             mm.isdel = chufangModel.isdel;
             mm.creatTime = chufangModel.creatTime;
             mm.uploadTime = chufangModel.uploadTime;
             mm.isSel = NO;
            }
        
         //保存
        [container.viewContext save:nil];
        [self tips:@"修改成功"];
    }
    else
    {
     NSLog(@"无检索");
    }
    
}

+ (void)addDataByPeopleModel:(People *)pModel {
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    NSPersistentContainer *container = delegate.persistentContainer;
    
    People * mm =   [NSEntityDescription insertNewObjectForEntityForName:@"People" inManagedObjectContext:container.viewContext];
    mm.cfmc = pModel.cfmc;
    mm.cflx = pModel.cflx;
    mm.pl = pModel.pl;
    mm.mk = pModel.mk;
    mm.zlsj = pModel.zlsj;
    mm.cxsj = pModel.cxsj;
    mm.jgsj = pModel.jgsj;
    mm.sssj = pModel.sssj;
    mm.xjsj = pModel.xjsj;
    mm.ycsj = pModel.ycsj;
    mm.isfscs = pModel.isfscs;
    mm.isbj = pModel.isbj;
    mm.isdel = pModel.isdel;
    mm.creatTime = pModel.creatTime;
    mm.uploadTime = pModel.uploadTime;
    mm.isSel = NO;
    
// ++++++++ 保存数据 ++++++++
    NSError *error = nil;
    BOOL success = [container.viewContext save:&error];
    if (!success) {
        [NSException raise:@"访问数据库错误！" format:@"%@", [error localizedDescription]];
    }else{
        [SVProgressHUD showSuccessWithStatus:@"保存数据库成功"];
        [SVProgressHUD dismissWithDelay:1.5];
        NSLog(@"访问数据库成功！");
    }
    
    
}

+ (void)addData {
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    NSPersistentContainer *container = delegate.persistentContainer;
    
    People * mm =   [NSEntityDescription insertNewObjectForEntityForName:@"People" inManagedObjectContext:container.viewContext];
//    mm.name = @"ly";
//    mm.age = @"38";
    
    // ++++++++ 保存数据 ++++++++
        NSError *error = nil;
        BOOL success = [container.viewContext save:&error];
        if (!success) {
            [NSException raise:@"访问数据库错误！" format:@"%@", [error localizedDescription]];
        }else{
            [SVProgressHUD showSuccessWithStatus:@"数据库成功"];
            [SVProgressHUD dismissWithDelay:1.5];
            NSLog(@"访问数据库成功！");
        }
    
    
}


+ (void)tips:(NSString *)str {
    [SVProgressHUD showSuccessWithStatus:str];
    [SVProgressHUD dismissWithDelay:1.5];
}

@end
